package com.ibm.risk.irmp.base.dict.rest;

import com.ibm.risk.irmp.base.dict.entity.BaseDictItem;
import com.ibm.risk.irmp.base.dict.service.IDataDictService;
import com.ibm.risk.irmp.base.dict.vo.BaseDictItemTreeVo;
import com.ibm.risk.irmp.base.dict.vo.BaseDictItemVo;
import com.ibm.risk.irmp.base.dict.vo.BaseDictVo;
import com.ibm.risk.irmp.common.auth.AuthUtils;
import com.ibm.risk.irmp.common.vo.CodeLabelVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/v2/dict")
@Tag(name = "DataDictController", description = "数据字典操作接口")
public class DataDictController {

    @Autowired
    private IDataDictService dataDictService;


    @Operation(summary = "get dicts for a group")
    @GetMapping(value = "/items/default/{groupId}")
    public BaseDictItemVo getDefaultItemByGroup(@PathVariable String groupId) {
        return dataDictService.getDefaultItemByGroup(groupId);
    }

    /**
     * 获取所有字典表信息
     */
    @GetMapping(value = "/dicts")
    public List<? extends BaseDictVo> getDictList() {
        Object user = AuthUtils.getLoginUser();
        log.info("loginUser: {}", user);
        List<? extends BaseDictVo> dataTypes = dataDictService.getDictList();
        return dataTypes;
    }

    @GetMapping(value = "/item-code-labels/dicts/{dictCode}")
    public List<CodeLabelVo> getItemCodeLabels(@PathVariable String dictCode, @RequestParam(required = false) String category) {
        return dataDictService.getItemCodeLabels(dictCode, category);
    }
    @GetMapping(value = "/item-code-label-map/dicts/{dictCode}")
    public Map<String, String> getItemCodeLabelMap(@PathVariable String dictCode, @RequestParam(required = false) String category) {
        Map<String, String> itemCodeLabelMap = dataDictService.getItemCodeLabelMap(dictCode, category);
        return itemCodeLabelMap;
    }

    @GetMapping(value = "/items/groups/{groupId}")
    public List<? extends BaseDictItemVo> getGroupItemList(@PathVariable String groupId) {
        return dataDictService.getGroupItemList(groupId);
    }


    @GetMapping(value = "/item-tree/dicts/{dictCode}")
    public List<BaseDictItemTreeVo> getItemTree(@PathVariable String dictCode) {
        return dataDictService.getItemTree(dictCode);
    }

    /**
     * 保存参数表
     */
    @PostMapping(value = "/dicts/saveDict")
    public Boolean saveDict(@RequestBody Map<String, Object> saveParmas) {
        Boolean result = dataDictService.saveDict(saveParmas);
        return result;
    }


    public Boolean saveDictItem(BaseDictItem entity) {
        return dataDictService.saveDictItem(entity);
    }


    public Boolean deleteDictItem(String id) {
        return dataDictService.deleteDictItem(id);
    }


    public Boolean deleteDict(Integer level, String dictType) {
        return dataDictService.deleteDict(level, dictType);
    }

    /**
     * 删除参数表
     */
    @DeleteMapping(value = "/dicts/deleteDict")
    public Boolean deleteDict(@RequestParam String dictType, @RequestParam Integer level) {
        Boolean result = dataDictService.deleteDict(level, dictType);
        return result;
    }

    /**
     * 保存参数代码
     */
    @PostMapping(value = "/items/saveDictItem")
    public Boolean save(@RequestBody BaseDictItem entity) {
        Boolean result = dataDictService.saveDictItem(entity);
        return result;
    }

    /**
     * 删除参数代码
     */
    @DeleteMapping(value = "/items/deleteItem")
    public Boolean deleteItem(@RequestParam String id) {
        Boolean result = dataDictService.deleteDictItem(id);
        return result;
    }
}
